//
//  ZTEidtViewController.m
//  CarTown
//
//  Created by xionghao on 15/9/14.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "ZTEidtViewController.h"
#import "Puser.h"
#import "CZTextView.h"
@interface ZTEidtViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *namelabel;
@property (weak, nonatomic)  CZTextView *textfield;
@property (weak, nonatomic) IBOutlet UIView *bgview;
@property (nonatomic,strong) Puser *user;
@end

@implementation ZTEidtViewController
+(instancetype)viewcontroller{
    ZTEidtViewController *vc=[[ZTEidtViewController alloc]initWithNibName:NSStringFromClass([self class]) bundle:nil];
    return vc;
}


-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    CZTextView *textfield = [[CZTextView alloc] initWithFrame:
                           CGRectMake(54+8, 5, XHScreenwidth-54-16, 36)];
    self.textfield=textfield;
    switch (self.type) {
        case 0:
            self.navigationItem.title=@"编辑联系人";
            self.namelabel.text=@"姓名";
            self.textfield.placeHolder=@"请输入姓名";
            break;
        case 1:
            self.navigationItem.title=@"编辑手机";
            self.namelabel.text=@"手机";
            self.textfield.placeHolder=@"请输入手机";
            break;
        case 2:
            self.navigationItem.title=@"编辑展厅地址";
            self.namelabel.text=@"地址";
            self.textfield.placeHolder=@"请输入地址";
            break;
        case 3:
            self.navigationItem.title=@"编辑展厅标题";
            self.namelabel.text=@"标题";
            self.textfield.placeHolder=@"请输入标题";
            break;
        default:
            break;
    }
    self.textfield.placehodercolor=UIColorWithRGB(217, 211, 215);
    self.textfield.font = [UIFont systemFontOfSize:14];
    self.textfield.text=self.text;
    self.namelabel.textColor=textnormalGary;
    self.namelabel.layer.borderColor=[BoderColor CGColor];
    self.namelabel.layer.borderWidth=0.5;
    self.textfield.textColor=textnormalGary;
    [self.bgview addSubview:self.textfield];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
    
    // 监听拖拽
    self.textfield.delegate = self;
    if (self.textfield.text.length>0) { // 有内容
        self.textfield.hidePlaceHolder = YES;
    }else{
        self.textfield.hidePlaceHolder = NO;
    }

    // Do any additional setup after loading the view from its nib.
}

-(void)textChange{
    // 判断下textView有木有内容
    if (self.textfield.text.length>0) { // 有内容
        self.textfield.hidePlaceHolder = YES;
    }else{
        self.textfield.hidePlaceHolder = NO;
    }
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)okclick:(id)sender {
    
    if ([self.text isEqualToString:self.textfield.text]) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        
        hud.labelText=@"正在修改...";
        
        hud.margin = 10.f;
        
        hud.removeFromSuperViewOnHide = YES;
        
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        
        NSInteger power=[self.user.power integerValue];
        if (power<5) {
            params[@"type"]=@"person";
            params[@"user_id"]=self.user.user_id;
        }else{
            params[@"type"]=@"company";
            params[@"user_id"]=self.user.com_id;
            if (power==8) {
                params[@"pid"]=self.user.parent_id;
            }
        }
        switch (self.type) {
            case  0:
             params[@"action"]=@"edit_display";
                params[@"username"]=self.textfield.text;
                break;
            case  1:
                params[@"action"]=@"edit_display";
                params[@"phone"]=self.textfield.text;
                break;
            case  2:
            params[@"action"]=@"edit_display";
                params[@"addr"]=self.textfield.text;
                break;
            case  3:
                params[@"action"]=@"edit_display";
                params[@"title"]=self.textfield.text;
                break;

            default:
                break;
        }
        [PostUitls Post:myzturl parameters:params success:^(id response) {
            NSDictionary *dict=response;
            hud.mode=MBProgressHUDModeText;
            hud.labelText=dict[@"msg"];
            NSNumber *state=dict[@"state"];
            if ([state integerValue]==400) {
                if ([_delegate respondsToSelector:@selector(PostOK:withtype:)]) {
                    [_delegate PostOK:self.textfield.text withtype:self.type];
                }
                [self.navigationController popViewControllerAnimated:YES];
            }
            [hud hide:YES afterDelay:1];
        } failure:^(NSError *error) {
            
            hud.mode=MBProgressHUDModeText;
             hud.labelText=@"网络错误";
            [hud hide:YES afterDelay:1];
        }];
    
       
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
